package com.sumer.common.core.result;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 响应结果码枚举
 *
 * @author sumer
 */
@Getter
@AllArgsConstructor
public enum ResultCode {

    // 通用响应码
    SUCCESS(200, "操作成功"),
    ERROR(500, "操作失败"),
    PARAM_ERROR(400, "参数错误"),
    UNAUTHORIZED(401, "未授权"),
    FORBIDDEN(403, "禁止访问"),
    NOT_FOUND(404, "资源不存在"),
    METHOD_NOT_ALLOWED(405, "请求方法不允许"),
    TOO_MANY_REQUESTS(429, "请求过于频繁"),

    // 用户相关
    USER_NOT_FOUND(1001, "用户不存在"),
    USER_ALREADY_EXISTS(1002, "用户已存在"),
    USER_PASSWORD_ERROR(1003, "密码错误"),
    USER_DISABLED(1004, "用户已被禁用"),
    USER_NOT_LOGIN(1005, "用户未登录"),
    USER_LOGIN_EXPIRED(1006, "登录已过期"),

    // 题目相关
    QUESTION_NOT_FOUND(2001, "题目不存在"),
    QUESTION_ALREADY_EXISTS(2002, "题目已存在"),
    QUESTION_CATEGORY_NOT_FOUND(2003, "题目分类不存在"),
    QUESTION_TYPE_ERROR(2004, "题目类型错误"),

    // 考试相关
    EXAM_NOT_FOUND(3001, "考试不存在"),
    EXAM_NOT_STARTED(3002, "考试未开始"),
    EXAM_ALREADY_FINISHED(3003, "考试已结束"),
    EXAM_TIME_OUT(3004, "考试时间已到"),
    EXAM_ALREADY_SUBMITTED(3005, "考试已提交"),

    // 记录相关
    RECORD_NOT_FOUND(4001, "记录不存在"),
    RECORD_ALREADY_EXISTS(4002, "记录已存在"),

    // 文件相关
    FILE_UPLOAD_ERROR(5001, "文件上传失败"),
    FILE_TYPE_ERROR(5002, "文件类型错误"),
    FILE_SIZE_ERROR(5003, "文件大小超限"),
    FILE_NOT_FOUND(5004, "文件不存在"),

    // 系统相关
    SYSTEM_ERROR(9001, "系统错误"),
    NETWORK_ERROR(9002, "网络错误"),
    DATABASE_ERROR(9003, "数据库错误"),
    CACHE_ERROR(9004, "缓存错误"),
    FEIGN_ERROR(9005, "服务调用失败"),
    SERVICE_UNAVAILABLE(9006, "服务不可用"),
    INTERNAL_SERVER_ERROR(9007, "服务器内部错误");

    /**
     * 响应码
     */
    private final Integer code;

    /**
     * 响应消息
     */
    private final String message;

    /**
     * 根据响应码获取枚举
     */
    public static ResultCode getByCode(Integer code) {
        for (ResultCode resultCode : values()) {
            if (resultCode.getCode().equals(code)) {
                return resultCode;
            }
        }
        return null;
    }
}